import { _decorator, Label, Node, Sprite } from 'cc';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
import { ListRender } from '../../Basic/Component/ListRender';
import { PropCellType } from './PropPanel';
import { PropModel } from './PropModel';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
const { ccclass, property } = _decorator;

@ccclass('PropCell')
export class PropCell extends ListRender {
  @property(Sprite)
  itemSpr: Sprite;
  @property(Label)
  itemCount: Label;
  @property(Node)
  itemAd: Node;
  @property(Node)
  itemBuy: Node;
  @property(Label)
  buyCoin: Label;

  curPropId: number;

  dataChanged(): void {
    if (this.data instanceof PropCellType) {
      this.curPropId = this.data.propId;
      const propKey = null;
      // switch (this.curPropId) {
      //   case 1 /** 蛋糕盒 */:
      //     propKey = GameParams.prop_1;
      //     break;
      //   case 2 /** 彩色蛋糕 */:
      //     propKey = GameParams.prop_2;
      //     break;
      //   case 3 /** 蛋糕铲(列) */:
      //     propKey = GameParams.prop_3;
      //     break;
      //   case 4 /** 蛋糕铲(行) */:
      //     propKey = GameParams.prop_4;
      //     break;
      // }
      console.log(propKey);
      // item.spriteFrame = ?

      this.buyCoin.string = `100 G`;
    }
  }

  onClickBuy() {
    console.log(this.buyCoin.string.match(/\d+/g));

    const money = Number(this.buyCoin.string.match(/\d+/g).shift());
    if (DataMgr.gameParams.get(GameParams.coin) >= money) {
      DataMgr.gameParams.set(GameParams.coin, DataMgr.gameParams.get(GameParams.coin) - money);
      // PropModel.Ins().addProp(this.curPropId, 1);
      EventManager.Ins().emit(EventType.REFRESH_COIN);
    } else {
      console.log('金币不足');
    }
  }

  onClickAd() {
    PropModel.Ins().adPropId = this.curPropId;
    // MultiPlatform.Ins().platformUnit.showVideo();
  }
}
